//
//  FNReadFileDetailsVC.m
//  CocoaLumberjackDemo
//
//  Created by ffq on 2023/3/13.
//  Copyright © 2023 shixueqian. All rights reserved.
//

#import "FQReadFileDetailsVC.h"
#import <WebKit/WebKit.h>

@interface FQReadFileDetailsVC ()<WKNavigationDelegate>

/** <#注释#> */
@property (nonatomic, strong) UIProgressView *progressView;

/** <#注释#> */
@property (nonatomic, strong) WKWebView *webView;

@end

@implementation FQReadFileDetailsVC

#pragma mark - life cycle
- (void)viewDidLoad{
    
    [super viewDidLoad];
    
    [self configNav];
    [self addControllers];
    [self layoutControllers];
    [self configControllers];
    [self initDatas];
    
    [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    
}

-(void)dealloc {
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
}

#pragma mark - UITableViewDelegate

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    self.progressView.hidden = NO;
    self.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);
    [self.view bringSubviewToFront:self.progressView];
}

- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    NSLog(@"加载失败");
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
        
//    // 设置字体
    NSString *fontFamilyStr = @"document.getElementsByTagName('body')[0].style.fontFamily='Arial';";
    [webView evaluateJavaScript:fontFamilyStr completionHandler:nil];
    //设置颜色
    [ webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= '#333333'" completionHandler:nil];
    [ webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.fontSize= '16pt'" completionHandler:nil];
    //修改字体大小
    [ webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '200%'"completionHandler:nil];
    
 }

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    
    if([keyPath isEqualToString:@"estimatedProgress"]) {
        self.progressView.progress = self.webView.estimatedProgress;
        NSLog(@"%.2f",self.webView.estimatedProgress);
        if(self.webView.estimatedProgress == 1) {
            __weak typeof (self)weakSelf = self;
            [UIView animateWithDuration:0.25 animations:^{
                weakSelf.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.4f);
            } completion:^(BOOL finished) {
                weakSelf.progressView.hidden = YES;
            }];
            
        }
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

#pragma mark - CustomDelegate

#pragma mark - event response
- (void)navRightBtnClick {
    
    NSData *data = [[NSData alloc]initWithContentsOfFile:self.path];
    NSString *text = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    
    NSDictionary *dic = @{@"value": text};
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请先充值？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okBtn = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:okBtn];
    
    [self presentViewController:alert animated:YES completion:nil];
    
}

#pragma mark - private methods

#pragma mark - private init methods

- (void)configNav{
    self.title = @"详情";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"上传" style:UIBarButtonItemStylePlain target:self action:@selector(navRightBtnClick)];
}

- (void)addControllers{
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.progressView];
    [self.view addSubview:self.webView];
}

- (void)layoutControllers{
    
}
- (void)configControllers{
    
}

- (void)initDatas{
    
    NSData *data = [[NSData alloc]initWithContentsOfFile:self.path];
    NSString *text = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    text = [text stringByReplacingOccurrencesOfString:@"[FQ]" withString:@"<br><br>[FQ]"];
    [self.webView loadHTMLString:text baseURL:nil];

   
  
}

#pragma mark - getters setters

- (WKWebView *)webView {
    if(!_webView) {
                
        _webView = [[WKWebView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        _webView.backgroundColor = [UIColor whiteColor];
        _webView.navigationDelegate = self;
        
    }
    return _webView;
}

- (UIProgressView *)progressView {
    if(!_progressView) {
        
        _progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.navigationController.navigationBar.frame), [UIScreen mainScreen].bounds.size.width, 1)];
        _progressView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);
        _progressView.hidden = YES;
    }
    return _progressView;
}

@end
